import DataManager from "../manager/DataManager";
import { privacyChannel } from "../Script/privacyChannel";
import { prv } from "../Script/prv";
import MainLayer from "./MainLayer";

const { ccclass, property } = cc._decorator;

@ccclass
export default class RewardPage extends cc.Component {
    @property(cc.Node)
    numberLevelLeft: cc.Node = null
    @property(cc.Node)
    rewardRecive: cc.Node = null
    @property(cc.Node)
    rewardBox: cc.Node = null

    @property(cc.Node)
    bottom: cc.Node = null

    @property(cc.Node)
    progress: cc.Node = null
    @property(cc.Node)
    powerIcon: cc.Node = null

    @property(cc.Node)
    toolIcon: cc.Node = null
    @property(cc.Node)
    powersmallIcon: cc.Node = null

    @property(cc.Node)
    toolsmallIcon: cc.Node = null

    protected onLoad(): void {
        if (cc.sys.localStorage.getItem("isRewardPower") == null) {
            cc.sys.localStorage.setItem("isRewardPower", 1)
        }
        if (Number(cc.sys.localStorage.getItem("isRewardPower") == 1)) {
            DataManager.instance.power += 1
            this.powerIcon.active = true
            this.toolIcon.active = false
            this.powersmallIcon.active = true
            this.toolsmallIcon.active = false
            DataManager.instance.save()
        } else {
            DataManager.instance.tool += 1
            this.powerIcon.active = false
            this.toolIcon.active = true
            this.powersmallIcon.active = false
            this.toolsmallIcon.active = true
            DataManager.instance.save()
        }
        console.error("DataManager.instance.level", DataManager.instance.level)
        if (privacyChannel.getChannel().getCtrl("p2")) {
            let style = {
                top: prv.getSysInfo().windowHeight / 2 + 270
            };
            prv.showInter(false, style);
        }
        if (!cc.sys.localStorage.getItem("passTimes")) { //从来没有领取过
            cc.sys.localStorage.setItem("passTimes", 1)
        } else {
            cc.sys.localStorage.setItem("passTimes", Number(cc.sys.localStorage.getItem("passTimes")) + 1)
        }

        this.progress.getComponent(cc.Sprite).fillRange = Number(cc.sys.localStorage.getItem("passTimes")) % 5 * 0.2;

        if ((DataManager.instance.level - 1) % 5 == 0) { //开箱子
            this.rewardRecive.getComponent(cc.Label).string = "x4"
            this.bottom.active = false
            this.rewardBox.getComponent(cc.Animation).play()
            this.progress.getComponent(cc.Sprite).fillRange = 1
            if (Number(cc.sys.localStorage.getItem("isRewardPower") == 1)) {
                DataManager.instance.power += 4
                DataManager.instance.save()
            } else {
                DataManager.instance.tool += 4
                DataManager.instance.save()
            }
            if (Number(cc.sys.localStorage.getItem("isRewardPower") == 1)) {
                cc.sys.localStorage.setItem("isRewardPower", 2)
            } else {
                cc.sys.localStorage.setItem("isRewardPower", 1)
            }


        } else {
            this.rewardBox.getComponent(cc.Animation).setCurrentTime(0)
            this.rewardBox.getComponent(cc.Animation).stop()

            this.numberLevelLeft.getComponent(cc.Label).string = "" + (5 - cc.sys.localStorage.getItem("passTimes") % 5)
        }
    }


    onClickNext() {
        if (privacyChannel.getChannel().getCtrl("p2")) {
            privacyChannel.getChannel().showInter(true)
        }
        this.node.parent.getChildByName("MainLayer").getComponent(MainLayer).freshTool()
        this.node.destroy()
    }
}
